<?php

namespace Acme\Likipe\BlogBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\Likipe\BlogBundle\Repository;

use Symfony\Component\HttpFoundation\RedirectResponse;

// Form
use Acme\Likipe\BlogBundle\Form\CategoryType;

// Entity
use Acme\Likipe\BlogBundle\Entity\Category;


class CategoryController extends Controller
{
    
    public function indexAction()
    {
    	$this->get( 'session' )
    		->setFlash( 'menu', '3' );
    	
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$oCategories = $oEm->getRepository( 'AcmeLikipeBlogBundle:Category' )
    		->findAll();
    	
        return $this->render( 'AcmeLikipeBlogBundle:Category:index.html.twig', 
        		array( 'categories' => $oCategories ));
    }
    
    public function addAction( Request $oRequest ){
    	$this->get( 'session' )
    		->setFlash( 'menu', '3' );
    	
    	$oForm = $this->createForm( new CategoryType(), new Category() );
    	
    	if ($oRequest->getMethod() == 'POST'){
    		$oForm->bindRequest( $oRequest );
    		
    		if ($oForm->isValid()){
    			$oEm = $this->getDoctrine()->getEntityManager();
    			
    			$oCategory = new Category();
    			$oCategory = $oForm->getData();
    			
    			$oEm->persist( $oCategory );
    			$oEm->flush();
    			
    			$this->get( 'session' )
    				->setFlash( 'notice', 'Add new Category success!' );
    			
    			return new RedirectResponse($this
    						->generateUrl( 'AcmeLikipeBlogBundle_category' ));
    		}
    	}
    	
    	return $this->render( 'AcmeLikipeBlogBundle:Category:add.html.twig',
    			array( 'form' => $oForm->createView() ));
    }
    
    public function deleteAction( $iId ){
    	$this->get( 'session' )
    		->setFlash( 'menu', '3' );
    		
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$oCategory = $oEm->getRepository( 'AcmeLikipeBlogBundle:Category' )
    		->find($iId);
    	 
    	if ($oCategory == null){
    		$this->get( 'session' )
    			->setFlash( 'notice', 'No found Category with id ' . 
    					$iId . '!' );
    		 
    		return new RedirectResponse( $this
    				->generateUrl( 'AcmeLikipeBlogBundle_category' ));
    	}
    	
    	$oEm->remove( $oCategory );
    	$oEm->flush();
    	 
    	$this->get( 'session' )
    		->setFlash( 'notice', 'Delete Category with id ' . 
    				$iId . ' success!' );
    	 
    	return new RedirectResponse( $this
    			->generateUrl( 'AcmeLikipeBlogBundle_category' ));
    }
    
    public function editAction( $iId, Request $oRequest ){
    	$this->get( 'session' )
    		->setFlash( 'menu', '3' );
    	
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$oCategory = $oEm->getRepository( 'AcmeLikipeBlogBundle:Category' )
    		->find( $iId );
    	
    	if ($oCategory == null){
    		$this->get( 'session' )
    			->setFlash( 'notice', 'No found Category with id ' . 
    					$iId . '!' );
    		 
    		return new RedirectResponse($this
    				->generateUrl( 'AcmeLikipeBlogBundle_category' ));
    	}
    	
    	$oForm = $this->createForm( new CategoryType(), $oCategory );
    	
    	if ($oRequest->getMethod() == 'POST'){
    		$oForm->bindRequest( $oRequest );
    	
    		if ($oForm->isValid()){
    			$oCategory = $oForm->getData();
    			 
    			$oEm->flush();
    			 
    			$this->get( 'session' )
    				->setFlash( 'notice', 'Edit Category success!' );
    			 
    			return new RedirectResponse( $this
    					->generateUrl( 'AcmeLikipeBlogBundle_category' ));
    		}
    	}
    	
    	return $this->render( 'AcmeLikipeBlogBundle:Category:edit.html.twig',
    			array( 'form' => $oForm->createView(),
    					'category' => $oCategory ));
    }
}
